home *** CD-ROM | disk | FTP | other *** search
/ Risc World 5 / Risc World 5.iso / SOFTWARE / Issue5 / PD / DIRSYNC / LegalStuff / ccres / c / Misc < prev    next >
Text File  |  2004-11-30  |  4KB  |  197 lines

  1. /* Misc.c
  2.    $Id: Misc.c,v 1.3 2004/11/30 00:51:56 joty Exp $
  3.  
  4.    Copyright (c) 2003-2004 Dave Appleby / John Tytgat
  5.  
  6.    This file is part of CCres.
  7.  
  8.    CCres is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License, or
  11.    (at your option) any later version.
  12.  
  13.    CCres is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with CCres; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  */
  22.  
  23. #include "ccres.h"
  24.  
  25. #include <stdio.h>        // sprintf
  26. #include <string.h>        // strncmp
  27.  
  28. #include <OSLib/taskmanager.h>
  29.  
  30.  
  31. // @@should read appname from _Messages file...
  32. wimp_t is_running(void)
  33. {
  34.     taskmanager_task task;
  35.     PSTR pszEnd;
  36.     int i;
  37.  
  38.     i = 0;
  39.     while ((i = taskmanager_enumerate_tasks(i, &task, sizeof(task), &pszEnd)) > 0) {
  40.         if (__strnicmp(task.name, APPNAME, sizeof(APPNAME) - 1) == 0 && task.name[sizeof(APPNAME) - 1] < ' ') {
  41.             return task.task;
  42.         }
  43.     }
  44.  
  45.     return NULL;
  46. }
  47.  
  48.  
  49. #ifdef DEBUG
  50. // use these for bounds checking and garbage collection during develop phase
  51.  
  52. static struct {
  53.     void * p;
  54.     int cb, nLine;
  55.     char achFile[MAX_FILE];
  56. } aAllocs[64];        // arbitary maximum no. of blocks
  57.  
  58. void MyAlloc_Init(void)
  59. {
  60.     memset(aAllocs, 0, sizeof(aAllocs));
  61. }
  62.  
  63. // check all memory blocks have been deleted OK, and report the creator of any remaining
  64. void MyAlloc_Report(void)
  65. {
  66.     int i;
  67.     char achBuff[128];
  68.  
  69.     for (i = 0; i < ELEMENTS(aAllocs); i++) {
  70.         if (aAllocs[i].p != NULL) {
  71.             sprintf(achBuff, "cb=%d File:%s Line:%d", aAllocs[i].cb, aAllocs[i].achFile, aAllocs[i].nLine);
  72.             errtitle(APPNAME": Memory leak", achBuff);
  73.         }
  74.     }
  75. }
  76.  
  77.  
  78. // allocate 12 extra bytes -
  79. // 1st word set to required block size (cb)
  80. // 2nd and last words set to 0x50515253
  81. void * My_Alloc(int cb, PSTR pszFile, int nLine)
  82. {
  83.     PSTR p;
  84.     int i;
  85.     char achBuff[128];
  86.  
  87.     if ((p = (PSTR) calloc(1, cb + 12)) == NULL) {
  88.         sprintf(achBuff, "%d bytes in file '%s' at line '%d'", cb, pszFile, nLine);
  89.         errtitle(APPNAME": Unable to allocate memory", achBuff);
  90.     } else {
  91.         *((int *) p) = cb;
  92.         for (i = 0; i < 4; i++) {
  93.             p[i + 4] = p[i + cb + 8] = (0x50 | i);
  94.         }
  95.         for (i = 0; i < ELEMENTS(aAllocs); i++) {
  96.             if (aAllocs[i].p == NULL) {
  97.                 aAllocs[i].p = p;
  98.                 aAllocs[i].cb = cb;
  99.                 aAllocs[i].nLine = nLine;
  100.                 strcpy(aAllocs[i].achFile, pszFile);
  101.                 break;
  102.             }
  103.         }
  104.         return (void *) (p + 8);
  105.     }
  106.     return NULL;
  107. }
  108.  
  109. // check for overwritten memory & report if changed
  110. void My_Free(void * v, PSTR pszFile, int nLine)
  111. {
  112.     PSTR p;
  113.     int cb, cbErr, i;
  114.     char achBuff[128];
  115.  
  116.     p = (PSTR) v;
  117.     p -= 8;
  118.     cb = *((int *) p);
  119.  
  120.     cbErr = 0;
  121.     for (i = 0; i < 4; i++) {
  122.         if (p[i + 4] != (0x50 | i)) {
  123.             if (cbErr == 0) {
  124.                 cbErr += sprintf(&achBuff[cbErr], "%s (%d)", pszFile, nLine);
  125.             }
  126.             cbErr += sprintf(&achBuff[cbErr], "lo[%d = %d]", i, p[i + 4]);
  127.         }
  128.         if (p[i + cb + 8] != (0x50 | i)) {
  129.             if (cbErr == 0) {
  130.                 cbErr += sprintf(&achBuff[cbErr], "%s (%d)", pszFile, nLine);
  131.             }
  132.             cbErr += sprintf(&achBuff[cbErr], "hi[%d = %d]", i, p[i + cb + 8]);
  133.         }
  134.     }
  135.     if (cbErr > 0) {
  136.         errtitle(APPNAME": Memory overwritten", achBuff);
  137.     }
  138.     free(p);
  139.     for (i = 0; i < ELEMENTS(aAllocs); i++) {
  140.         if (aAllocs[i].p == p) {
  141.             aAllocs[i].p = NULL;
  142.             break;
  143.         }
  144.     }
  145. }
  146.  
  147. #else
  148.  
  149. void * My_Alloc(int cb, PSTR pszFile, int nLine)
  150. {
  151.     PSTR p;
  152.     char achBuff[128];
  153.  
  154.     if ((p = (PSTR) calloc(1, cb)) == NULL) {
  155.         sprintf(achBuff, "%d bytes in file '%s' at line '%d'", cb, pszFile, nLine);
  156.         errtitle(APPNAME": Unable to allocate memory", achBuff);
  157.     }
  158.     return p;
  159. }
  160.  
  161. #endif
  162.  
  163. #ifdef DEBUG
  164.  
  165. #include <stdarg.h>
  166.  
  167. static BOOL fLog = FALSE;
  168. // static char achLog[] = APPDIR".log";
  169. static char achLog[] = "RAM::0.$.ccres_log";
  170. void log_on(void)
  171. {
  172.     FILE * hLog;
  173.  
  174.     if ((hLog = fopen(achLog, "w")) != NULL) {
  175.         fclose(hLog);
  176.         fLog = TRUE;
  177.     } else {
  178.         error("Unable to create log file");
  179.     }
  180. }
  181.  
  182. void log_it(PSTR szFmt, ...)
  183. {
  184.     FILE * hLog;
  185.     va_list list;
  186.  
  187.     if (fLog && (hLog = fopen(achLog, "a")) != NULL) {
  188.         va_start(list, szFmt);
  189.         vfprintf(hLog, szFmt, list);
  190.         va_end(list);
  191.         fputc('\n', hLog);
  192.         fclose(hLog);
  193.     }
  194. }
  195.  
  196. #endif    // DEBUG
  197.